/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Supermarket;

import CustomerGUIs.NewCustomerForm;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.persistence.EntityManager;

/**
 *
 * @author Manolis
 */
public class Supermarket {
    // O entity manager 
    private static EntityManager em, em_ex ;
    private static Connection conn1, conn2;
    private static String internaldbURL = 
            "jdbc:derby://localhost:1527/3h_Ergasia_database;user=plh24;password=plh24";
    private static String externaldbURL = 
            "jdbc:derby://plh24.ecomet.eap.gr:8080/CREDIT_CARD_AUTH_DB;user=student;password=studentpass";
   
            
    //Σύνδεση στην τοπική ΒΔ.
    private static boolean createConnectionToInternalDB() {
        boolean success = false;
        try {
            Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
            //Get a connection
            conn1 = DriverManager.getConnection(internaldbURL);
            Supermarket.em = javax.persistence.Persistence.createEntityManagerFactory
                                            ("plh24_3h_ErgasiaPU").createEntityManager();
            success = true;
 
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | SQLException except) {
            System.out.println("ERROR: " + except.getMessage());
        }
        return success;
    }
    
     //Δημιουργεί μια σύνδεση με την εξωτερική ΒΔ
    private static void createConnectionToExternalDB() {
        try {
            Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
            //συνδέεται στην εξωτερική ΒΔ
            conn2 = DriverManager.getConnection(externaldbURL);
            //Δημιουργεί έναν entity manager
            em_ex = javax.persistence.Persistence.createEntityManagerFactory("plh24_3h_ErgasiaPU2").
                createEntityManager();
            
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | SQLException except) {
            System.out.println("ERROR: " + except.getMessage());
        }
    }
    
    

    public static void shutdown() {
        try {
            if (conn1 != null) {
                DriverManager.getConnection(internaldbURL + ";shutdown=true");
                conn1.close();
            }
            
        } catch (SQLException sqlExcept) {
        }
    }   
    
    //επιστρέφει τον entity manager 
    public static EntityManager getEm() {
        return em;
    }
    
    
    //επιστρέφει τον κατάλληλο entity manager 
    public static EntityManager getEm(String direction) {
        //εξ ορισμού επιστρέφει τον em της εσωτερικής ΒΔ
        EntityManager EM = em;
        //αν ζητηθεί επιστρέφει της εξωτερικής
        if ("external".equals(direction))
            EM = em_ex;
        return EM;
    }
    
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        
        boolean success = createConnectionToInternalDB();
                
        //τέστ φόρμας
        NewCustomerForm newCustomerFrame = new NewCustomerForm();
        //δημιουργείται ένας αριθμός κάρτας
        newCustomerFrame.setPointsCardLabel(newCustomerFrame.makeCardNumber());
        //εμφανίζεται η φόρμα εισαγωγής πελατών
        newCustomerFrame.setVisible(true);
    }
    
}
